import Vue from 'vue'
import Router from 'vue-router'
import routes from './routes'
import { judgeLogin, judgeAdminAuth } from '@/util/auth' 
import { USER, SEARCH } from './name'
// test
Vue.use(Router)

const router =  new Router({
  // mode: "history",
  // base: process.env.BASE_URL,
  routes,
})

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // this route requires auth, check if logged in
    // if not, redirect to login page.
    if (!judgeLogin()) {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    } else {
      //如果进入user页面，则判断是否有管理员权限
      if(to.name != USER || judgeAdminAuth()){
        next()
      }else{
        next({
          name: SEARCH,
        })     
      }
    }
  } else {
    next() // 确保一定要调用 next()
  }
})

export default router;